<?php


namespace Api\Http\Controllers;


use Api\traits\ApiResponse;
use Catch\Traits\DB\BaseOperate;
use EasyWeChat\Kernel\Exceptions\DecryptException;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use Illuminate\Http\Request;
use Api\Http\Controllers\Controller;
use EasyWeChat\MiniApp\Application;
use Modules\Member\Models\MembersAddress;


/**
 * @group 用户模块
 *
 * @subgroup 用户管理
 * @subgroupDescription CatchAdmin 后台用户管理
 *
 */
class MemberAddressController extends Controller
{
    use ApiResponse;

    public function __construct(
        protected readonly MembersAddress $membersAddress
    )
    {

    }

    /**
     * 用户列表
     *
     * @queryParam department_id int 部门
     * @queryParam page int 页码
     * @queryParam limit int 每页条数
     * @queryParam Membername string 关键字
     * @queryParam email string 邮箱
     * @queryParam status int 状态
     *
     */
    public function index()
    {

        $list = $this->membersAddress::where('user_id', $this->getLoginUserId())
            ->orderBy('is_default', 'desc')
            ->orderBy('id', 'desc')->get();
        return $this->success('获取成功', $list->toArray());
    }

    /**
     * 新增用户
     *
     * @bodyParam Membername string required 用户名
     * @bodyParam password string required 密码
     * @bodyParam email string 邮箱
     * @bodyParam mobile string 手机号
     * @bodyParam department_id int 部门
     * @bodyParam roles integer[] 角色
     * @bodyParam jobs integer[] 职位
     *
     * @responseField data int 新增ID
     *
     * @return false|mixed
     * @throws InvalidArgumentException
     */
    public function store(Request $request)
    {
        $data = $request->all();
        if(empty($data['send_city']) || empty($data['send_county'])){
            return  $this->fail('请手动选择省市区');
        }
        if(empty($data['detail'])){
            return  $this->fail('请填写详细地址');
        }
        $data['user_id'] = $this->getLoginUserId();
        if ($data['is_default'] == 1) {
            //清除用户，默认状态

            $this->membersAddress->where('user_id', $this->getLoginUserId())->update(['is_default' => '0']);
        }
        if ($this->membersAddress->create($data)) {
            return $this->success('保存成功');
        } else {
            return $this->fail('保存失败');
        }
    }



    /**
     * 更新用户
     *
     * @urlParam id int required 用户ID
     *
     * @bodyParam Membername string required 用户名
     * @bodyParam password string 密码
     * @bodyParam email string 邮箱
     * @bodyParam mobile string 手机号
     * @bodyParam department_id int 部门
     * @bodyParam roles integer[] 角色
     * @bodyParam jobs integer[] 职位
     *
     * @responseField data bool 是否更新成功
     *
     * @param $id
     * @param OrderRequest $request
     * @return mixed
     */
    public function update($id, Request $request): mixed
    {

        if ($request->post('is_default') == '1') {
            //清除用户，默认状态

            $this->membersAddress->where('user_id', $this->getLoginUserId())->update(['is_default' => '0']);
        }
        if ($this->membersAddress->where('id', $id)->update($request->post())) {
            $count = $this->membersAddress->where('user_id', $this->getLoginUserId())->where('is_default', '1')->count();
            if ($count == 0) {
                $first = $this->membersAddress->where('user_id', $this->getLoginUserId())->first();
                $first->is_default = '1';
                $first->save();
            }
            return $this->success('修改成功');
        } else {
            return $this->fail('修改失败');
        }

    }

    /**
     * 删除用户
     *
     * @responseField data bool 是否更新成功
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if ($this->membersAddress->where('id', $id)->delete()) {
            $count = $this->membersAddress->where('user_id', $this->getLoginUserId())->where('is_default', '1')->count();
            if ($count == 0) {
                $first = $this->membersAddress->where('user_id', $this->getLoginUserId())->first();
                if(!empty($first)){
                    $first->is_default = '1';
                    $first->save();
                }

            }
            return $this->success('删除成功');
        } else {
            return $this->fail('删除失败');
        }
    }


}
